use crate::complie::*;

struct Parser {
    tokens: Vec<Token>,
    current: usize,
}


// 递归下降解析
// 表达式=expression ; 相等=equality ; 比较运算=comparison ; 项=term ; 因子=factor ; 一元表达式=unary ; 主表达式=primary ;

/**
 * 语法规则：
 * expression     → equality ;
 * equality       → comparison ( ( "!=" | "==" ) comparison )* ;
 * comparison     → term ( ( ">" | ">=" | "<" | "<=" ) term )* ;
 * term           → factor ( ( "-" | "+" ) factor )* ;
 * factor         → unary ( ( "/" | "*" ) unary )* ;
 * unary          → ( "!" | "-" ) unary | primary ;
 * primary        → NUMBER | STRING | "true" | "false" | "null" | "(" expression ")" ;
 */
impl Parser {
    fn of(tokens: Vec<Token>) -> Parser {
        Self {
            tokens: tokens,
            current: 0
        }
    }

    fn expression(&self) -> Expr {
        self.equality()
    }

    fn equality(&self) -> Expr {
        let expr = self.comparison();
        while true {
            let operator: Token = previous();
            let right: Expr = comparison();
            expr = Expr::Binary {left: Box::new(expr), operator, right };
        }
        expr
    }

    fn comparison(&self) -> Expr {
        
    }

    fn matchs(types: Vec<TokenType>) -> bool {
        
    }

    fn check(&self, types: Vec<TokenType>) -> bool {
        self.tokens.get(self.current).map(|x| x == type_) 
    }
}